﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using TM.QWeibo.SDK.Objects;
using TM.QWeibo.Common;

namespace TM.QWeibo.Dal
{

    /// <summary>
    /// 微博 
    /// </summary>
    public class Tweet: Base
    {
        #region 私有属性
        /// <summary>
        /// API接口
        /// </summary>
        readonly SDK.API.Twitter _api = new SDK.API.Twitter(OAuthInstance);

        private Action<SDK.Objects.Tweet> getTweetCallback { get; set; }

        private Action<ICollection<SDK.Objects.Tweet>> getReplyCallback { get; set; }

        /// <summary>
        /// 发布微博回调
        /// </summary>
        private Action<bool> publishTweetCallback { get; set; }

        #endregion

        /// <summary>
        /// 实例
        /// </summary>
        public static Tweet Instance {
            get {
                return new Tweet();
            }
        }


        #region 私有方法

        /// <summary>
        /// 加载回调
        /// </summary>
        /// <param name="rs"></param>
        private void GetTweetCallback(SDK.Objects.ResponseDataBase<SDK.Objects.Tweet> rs)
        {
            if (getTweetCallback != null) {
                getTweetCallback(rs.Data ?? (new SDK.Objects.Tweet()));
                //Save Tweet
                if (rs.Data != null)
                {
                    Cache.SterlingService.DataBase.Save(rs.Data);
                    Cache.SterlingService.DataBase.Flush();
                }
            }
        }

        /// <summary>
        /// 加载回复的回调
        /// </summary>
        /// <param name="rs"></param>
        private void GetReplyCallback(SDK.Objects.ResponseData<SDK.Objects.Tweet> rs)
        {
            if (getReplyCallback != null)
            {
                var ts=new List<SDK.Objects.Tweet>();
                if (rs.Data != null && rs.Data.Info != null) {
                    ts = rs.Data.Info;
                }
                getReplyCallback(ts);
            }
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 获取一条微博
        /// </summary>
        /// <param name="tweetId"></param>
        /// <param name="callback"></param>
        public void Get(string tweetId, Action<SDK.Objects.Tweet> callback)
        {
            var datas = Cache.SterlingService.DataBase.Query<SDK.Objects.Tweet, string>();
            foreach (var tableKey in datas.Where(tableKey => string.Equals(tweetId,tableKey.Key)))
            {
                callback(tableKey.LazyValue.Value);
                return;
            }
            getTweetCallback = callback;
            _api.GetTweet(tweetId, GetTweetCallback);
        }
        /// <summary>
        /// 发表新微博
        /// </summary>
        /// <param name="tweetContent">微博内容，text属性不能为空</param>
        /// <param name="callback">发布结束后的回调方法</param>
        public void Add(SDK.Objects.TweetContent tweetContent, Action<bool> callback,bool syncToQzone=false) {
            if (!string.IsNullOrWhiteSpace(tweetContent.Text))
            {
                publishTweetCallback = callback;
                _api.Add(tweetContent, PublishTweetEnd,syncToQzone);
            }
            else {
                throw new Exception("微博内容不能为空！");
            }
        }

        /// <summary>
        /// 转播微博
        /// </summary>
        /// <param name="targetId">转播的微博ID</param>
        /// <param name="tweetContent">微博内容</param>
        /// <param name="callback">发布结束后的回调方法</param>
        public void ReAdd(string targetId, SDK.Objects.TweetContent tweetContent, Action<bool> callback, bool syncToQzone = false)
        {
            publishTweetCallback = callback;
            _api.ReAdd(targetId, tweetContent.Text, tweetContent.ClientIp, tweetContent.Jing, tweetContent.Wei, PublishTweetEnd, syncToQzone);
        }

        /// <summary>
        /// 回复/评论微博
        /// </summary>
        /// <param name="targetId">转播的微博ID</param>
        /// <param name="tweetContent">微博内容，text属性不能为空</param>
        /// <param name="callback">发布结束后的回调方法</param>
        public void Reply(string targetId, SDK.Objects.TweetContent tweetContent, Action<bool> callback, bool syncToQzone = false)
        {
            if (!string.IsNullOrWhiteSpace(tweetContent.Text))
            {
                publishTweetCallback = callback;
                _api.Reply(targetId, tweetContent.Text, tweetContent.ClientIp, tweetContent.Jing, tweetContent.Wei, PublishTweetEnd, syncToQzone);
            }
            else
            {
                throw new Exception("微博内容不能为空！");
            }
        }

        /// <summary>
        /// 对话微博
        /// </summary>
        /// <param name="targetId">转播的微博ID</param>
        /// <param name="tweetContent">微博内容，text属性不能为空</param>
        /// <param name="callback"></param>
        public void Comment(string targetId, SDK.Objects.TweetContent tweetContent, Action<bool> callback,bool syncToQzone = false)
        {
            if (!string.IsNullOrWhiteSpace(tweetContent.Text))
            {
                publishTweetCallback = callback;
                _api.Comment(targetId, tweetContent.Text, tweetContent.ClientIp, tweetContent.Jing, tweetContent.Wei, PublishTweetEnd, syncToQzone);
            }
            else
            {
                throw new Exception("微博内容不能为空！");
            }
        }

        /// <summary>
        /// 删除微博
        /// </summary>
        /// <param name="targetId">微博ID</param>
        /// <param name="callback">结束后的回调方法</param>
        public void Delete(string targetId, Action<bool> callback)
        {
            publishTweetCallback = callback;
            _api.Delete(targetId, PublishTweetEnd);
        }
        /// <summary>
        /// 操作回调
        /// </summary>
        /// <param name="rs"></param>
        private void PublishTweetEnd(SDK.Objects.ResponseData<SDK.Objects.TweetOperateResult> rs)
        {
            if (publishTweetCallback != null)
            {
                var b=rs.Errcode == SDK.Objects.EErrcode.Success;
                publishTweetCallback(b);
            }
        }

        /// <summary>
        /// 获取回复转播列表
        /// </summary>
        /// <param name="pageflag"></param>
        /// <param name="pagetime"></param>
        /// <param name="rootId"> 微博ID</param>
        /// <param name="tweetId"></param>
        /// <param name="callback"></param>
        public void ReplyList(PageFlag pageflag, long pagetime,string rootId, string tweetId, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            getReplyCallback = callback;
            _api.GetReList(pageflag, pagetime, Params.PageListCount, rootId, tweetId, GetReplyCallback);
        }

        /// <summary>
        /// 收藏一条微博
        /// </summary>
        /// <param name="id"></param>
        /// <param name="callback"></param>
        public void AddFavorite(string id, Action<bool> callback)
        {
            _api.AddFavorite(id, (rs) => {
                callback(rs.Errcode == EErrcode.Success);
            });
        }

        /// <summary>
        /// 取消收藏一条微博
        /// </summary>
        /// <param name="id"></param>
        /// <param name="callback"></param>
        public void DelFavorite(string id, Action<bool> callback)
        {
            _api.DelFavorite(id, (rs) =>
            {
                callback(rs.Errcode == EErrcode.Success);
            });
        }

        /// <summary>
        ///搜
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageNum"></param>
        /// <param name="callback"></param>
        public void Search(string keyword, int pageNum, Action<SDK.Objects.ResponseDataObject<SDK.Objects.Tweet>> callback)
        {
            _api.Search(keyword, Common.Params.PageListCount, pageNum, callback);
        }

        #endregion

    }
}
